package com.rjwh.dingdong.client.activity.more;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.orangegangsters.github.swipyrefreshlayout.library.SwipyRefreshLayout;
import com.orangegangsters.github.swipyrefreshlayout.library.SwipyRefreshLayoutDirection;
import com.rjwh.dingdong.client.R;
import com.rjwh.dingdong.client.activity.BaseActivity;
import com.rjwh.dingdong.client.activity.community.CommunityPostDetailActivity;
import com.rjwh.dingdong.client.adapter.TopicFavoriteAdapter;
import com.rjwh.dingdong.client.application.MyApplication;
import com.rjwh.dingdong.client.bean.localbean.TopicFavoriteListData;
import com.rjwh.dingdong.client.constant.LocalConstant;
import com.rjwh.dingdong.client.mvp.presenter.impl.MoreCollectionPresenter;
import com.rjwh.dingdong.client.mvp.view.MoreCollectionView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author Kay
 * @ClassName MoreCollectionActivity
 * @Description
 * @date 2016-7-21
 * @history 2016-7-21 author: description:
 */
public class MoreCollectionActivity extends BaseActivity implements MoreCollectionView {

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.app_bar_layout)
    AppBarLayout appBarLayout;
    @BindView(R.id.more_collection_listview)
    ListView moreCollectionListview;
    @BindView(R.id.more_collection_mPullRefreshView)
    SwipyRefreshLayout moreCollectionMPullRefreshView;
    private int pageNumber = 0;
    private TopicFavoriteAdapter adapter;

    private MoreCollectionPresenter presenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.more_collection_layout);
        ButterKnife.bind(this);
        initView();
        presenter = new MoreCollectionPresenter(this, this);
        moreCollectionMPullRefreshView.doPullRefreshing();
    }

    private void initView() {
        initToolbarHelper();
        moreCollectionMPullRefreshView.setOnRefreshListener(new SwipyRefreshLayout.OnRefreshListener() {

            @Override
            public void onRefresh(SwipyRefreshLayoutDirection direction) {
                switch (direction) {
                    case TOP:
                        pageNumber = 0;
                        getTopicFavorite(true,SwipyRefreshLayoutDirection.TOP);
                        break;

                    case BOTTOM:
                        pageNumber++;
                        getTopicFavorite(false,SwipyRefreshLayoutDirection.BOTTOM);
                        break;

                    default:
                        break;
                }
            }
        });
        adapter = new TopicFavoriteAdapter(this);
        moreCollectionListview.setAdapter(adapter);
        moreCollectionListview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                TopicFavoriteListData topicItem = (TopicFavoriteListData) parent.getAdapter().getItem(position);
                if (topicItem == null) {
                    return;
                }
                String filepath = topicItem.getFilepath();
                Intent intent = new Intent(MoreCollectionActivity.this, CommunityPostDetailActivity.class);
                if (topicItem.getTopicid() != null) {
                    intent.putExtra("topicid", topicItem.getTopicid());
                }
                if (filepath != null) {
                    String url = filepath + "?userid=" + MyApplication.spUtil.getStrPreferenceByParamName(MyApplication.app, LocalConstant.SP_USERID) + "&platform=ANDROID" + "&version=" + MyApplication.versionCode + "&udid=" + MyApplication.deviceId + "&babyid=" + MyApplication.spUtil.getStrPreferenceByParamName(MyApplication.app, LocalConstant.SP_BABYID) + "&token=" + MyApplication.spUtil.getStrPreferenceByParamName(MyApplication.app, LocalConstant.SP_USER_TOKEN);
                    intent.putExtra("url", url);
                }
                startActivity(intent);
            }
        });
    }

    private void getTopicFavorite(boolean isRefresh, SwipyRefreshLayoutDirection direction) {
        presenter.getTopicFavorite(pageNumber, isRefresh, direction);
    }

    private void initToolbarHelper() {
        this.setSupportActionBar(this.toolbar);
        getSupportActionBar().setTitle(getResources().getString(R.string.more_collection));
        getSupportActionBar().setHomeButtonEnabled(true); //设置返回键可用
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
    }

    @Override
    public void setDirection(SwipyRefreshLayoutDirection direction) {
        moreCollectionMPullRefreshView.setDirection(direction);
    }

    @Override
    public void setRefreshing(boolean refreshing) {
        moreCollectionMPullRefreshView.setRefreshing(refreshing);
    }

    @Override
    public void loadData(boolean isRefresh, List<TopicFavoriteListData> topiclist) {
        if (isRefresh) {
            adapter.clear();
        }
        adapter.appendToList(topiclist);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        presenter.detachView();
    }
}
